<template>
  <div id="app" class="app-page">
    <root>
      <safe-header
        v-bind="{
          title: '产成品出库',
          isBack: true,
          backIconColor: '#fff',
          fixedTop: true,
        }"
      />
      <div id="PickingByMrl">
        <v-container class="py-6 px-2">
          <v-row class="mt-2">
            <v-col :cols="12" pb-0>
              <v-text-field
                v-model="agreementNo"
                solo
                autofocus
                clearable
                hide-details
                placeholder="请输入履约号查询"
                @keydown.13="codeSearch"
                style="font-size: 14px"
              ></v-text-field>
            </v-col>
          </v-row>
          <v-row class="mt-2" style="height: 48px">
            <v-col :cols="12" pb-0>
              <v-select
                :items="areaInfoList"
                autofocus
                clearable
                solo
                placeholder="选择库区"
                item-text="areaName"
                item-value="erpAreaId"
                v-model="erpAreaId"
                @change="getSelected(erpAreaId)"
              ></v-select>
            </v-col>
          </v-row>
        </v-container>
        <v-container white class="pa-2 OutboundReview_bottom">
          <v-row>
            <v-col cols="12" class="d-flex align-center">
              <span class="card-title">待拣任务</span>
              <span class="card-title ml-2">
                {{
                pickTaskList.length == 0 ? "" : ": " + pickTaskList.length
                }}
              </span>
            </v-col>
          </v-row>
          <v-divider class="mb-2"></v-divider>
          <div v-if="pickTaskList[0]">
            <v-container v-for="(item, index) in pickTaskList" :key="index" class="px-1">
              <v-row>
                <v-col cols="12" class="pa-0 pb-2">
                  <v-card outlined style="background-color: #f2f3f5">
                    <v-card-subtitle class="list_content pa-1">
                      <v-row>
                        <v-col cols="2" class="pr-2 d-flex justify-center align-center">
                          <v-row>
                            <v-col class="pr-2 d-flex justify-center align-center">
                              <span>{{ index + 1 }}</span>
                            </v-col>
                            <v-checkbox v-model="item.isCheck"></v-checkbox>
                          </v-row>
                          <v-divider vertical></v-divider>
                        </v-col>

                        <v-col cols="10">
                          <v-row class="d-flex flex-row justify-space-between wms-v-row">
                            <v-col cols="12" class="card-lable">发货单号：{{ item.soMasterCode }}</v-col>
                          </v-row>
                          <v-row class="d-flex flex-row justify-space-between wms-v-row">
                            <v-col cols="12" class="card-lable">物料编码：{{ item.itemCode}}</v-col>
                          </v-row>
                          <v-row class="d-flex flex-row justify-space-between wms-v-row">
                            <v-col cols="12" class="card-lable">物料名称：{{ item.itemName }}</v-col>
                          </v-row>
                          <v-row class="d-flex flex-row justify-space-between wms-v-row">
                            <v-col cols="12" class="card-lable">规格型号：{{ item.itemMode }}</v-col>
                          </v-row>
                          <v-row class="pa-0 d-flex flex-row justify-space-between wms-v-row">
                            <v-col cols="8" class="card-lable">库位：{{ item.cellCode }}</v-col>
                            <v-col class="card-value">数量：{{ item.pickQuantity }}</v-col>
                          </v-row>
                        </v-col>
                      </v-row>
                    </v-card-subtitle>
                  </v-card>
                </v-col>
              </v-row>
            </v-container>
          </div>
          <v-row v-else>
            <v-col cols="12" class="text-center">暂无数据</v-col>
          </v-row>
        </v-container>
        <div v-show="hidshow" class="footer">
          <v-bottom-navigation height="60" style="background: transparent; box-shadow: none">
            <v-row class="pa-0 ma-0 justify-center" style="background: transparent">
              <v-col
                cols="12"
                class="pa-0 ma-0 d-flex justify-center align-center mb-2 px-2 py-1"
                style="background: transparent"
              >
                <v-btn
                  class="wms-submit-btn"
                  dark
                  block
                  color="#193690"
                  style="
                    height: 100%;
                    border-radius: 10px;
                    color: #fff !important;
                  "
                  @click="OutboundSubmit"
                >提交</v-btn>
              </v-col>
            </v-row>
          </v-bottom-navigation>
        </div>
      </div>
    </root>
    <v-overlay :value="overlay">
      <v-progress-circular indeterminate size="64"></v-progress-circular>
    </v-overlay>
  </div>
</template>

<script>
import SafeHeader from "../../components/safeHeader_wms";
import Root from "@/components/Root.vue";
import { mdiChevronRight, mdiCreditCardScan } from "@mdi/js";
import wms from "@/utils/business_wms";

export default {
  name: "PickingByMrl",
  components: {
    SafeHeader,
    Root
  },
  data() {
    return {
      docmHeight: document.documentElement.clientHeight,
      showHeight: document.documentElement.clientHeight,
      hidshow: true,
      overlay: false,
      alert: false,
      soMasterCode: "",
      svgpath: mdiChevronRight,
      mdiCreditCardScan: mdiCreditCardScan,

      agreementNo: "", // 履约单号
      pickTaskList: [
        //拣货单列表
        //{
        //  itemCode: "IM21102-918-123",
        //  itemName: "物料名称",
        //  itemMode: "x123xx",
        //  cellCode: "所属库位",
        //  soMasterCode: "ABC1111111",
        //  pickQuantity: 110,
        //  isCheck: null,
        //},
      ],

      // 当前出库单据类型--是否为调拨出库
      isCell: false,
      // 库区
      areaInfoList: [],
      erpAreaId: "",
    };
  },

  mounted() {
    window.onresize = () => {
      return (() => {
        this.showHeight = document.body.clientHeight;
      })();
    };

    wms
      .get({
        url: "/area/infos/list",
        data: { pageSize: 999 }
      })
      .then(res => {
        // console.log(JSON.stringify(res.data.data.list));
        if (res.status == 200 && res.data.code == 200) {
          let retData = res.data.data.list;
          let newArray = [];
          console.log(JSON.stringify(retData));
          retData.forEach(item => {
            //此处如果需要留下重复元素,则将!去掉即可
            //arr中的元素如果在ids中不存在,则添加到新数组中
            let itemName = item.areaName;
            if (itemName.indexOf("发出商品") != -1) {
              newArray.push(item);
            }
          });

          this.areaInfoList = newArray;
        } else {
          alert(res.data.message);
        }
      });
  },

  watch: {
    showHeight: function() {
      if (this.docmHeight > this.showHeight) {
        this.hidshow = false;
      } else {
        this.hidshow = true;
      }
    }
  },

  methods: {
    //查询履约号对应的所有出库任务并进行推荐
    codeSearch() {
      this.overlay = true;
      wms
        .get({
          url: "/so/masters/suggestPickAndGet",
          data: { agreementNo: this.agreementNo }
        })
        .then(res => {
          console.log(JSON.stringify(res));
          if (res.status == 200 && res.data.code == 200) {
            //
            this.pickTaskList = res.data.data;
            //console.log(JSON.stringify(this.pickTaskList));
            this.overlay = false;
          } else {
            alert(res.data.message);
            this.overlay = false;
          }
        });
    },

    // 下拉框选值
    getSelected(val) {
      this.erpAreaId = val;
      console.log(this.erpAreaId);
    },

    // 提交
    OutboundSubmit() {
      //校验数据
      if (this.pickTaskList.length == 0) {
        return;
      }
      console.log(JSON.stringify(this.pickTaskList));
      let pickIds = [];
      for (const index in this.pickTaskList) {
        if (this.pickTaskList[index].isCheck) {
          pickIds.push(this.pickTaskList[index].pickTaskId);
        }
      }
      console.log(JSON.stringify(pickIds));
      if (!this.erpAreaId) {
        return;
      }

      if (pickIds.length == 0) {
        return;
      }

      //提交数据

      let params = {
        // wareId: localStorage.getItem("wareId"),
        soMasterId: this.soMasterCode,
        pickIds: pickIds.join(","),
        erpAreaId: this.erpAreaId
      };
      console.log(JSON.stringify(params));
      this.overlay = true;
      wms
        .get({
          url: "/pick/tasks/pickAndSendOut",
          data: params
        })
        .then(res => {
          console.log(JSON.stringify(res));
          if (res.status == 200 && res.data.code == 200) {
            this.overlay = false;
            //
            alert("提交成功");
            window.location.reload();
          } else {
            alert(res.data.message);
            this.overlay = false;
          }
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.footer {
  position: absolute;
  padding-top: 65px;
  width: 100%;
  bottom: 0;
}

#app {
  .row {
    margin: 0;
  }
  .col {
    padding: 0;
  }
}
#OutboundReview {
  height: 100%;
  display: flex;
  flex-direction: column;
  .OutboundReview_bottom {
    flex: 1;
    padding-bottom: 68px !important;
  }
}
</style>
